package plugin;

import java.awt.Color;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JComponent;
import javax.swing.Timer;

public class Blinker {
	protected int rounds = 1;
	private JComponent c;
	private Timer blinktimer;
	private Color color;
	private Color normalcolor;
	private boolean foreground_mode;

	protected Blinker(JComponent c) {
		this(c,new Color(186, 255, 186),false);
	}
	protected Blinker(JComponent c, Color blinkColor, boolean foreground) {
		this.c = c;
		this.foreground_mode = foreground;
		this.normalcolor = foreground?c.getForeground():c.getBackground();
		this.color=blinkColor;
		this.blinktimer = new Timer(800, new ActionListener() {
			
			@Override
			public void actionPerformed(ActionEvent evt) {
				fireAction();
			}
		});
		blinktimer.setInitialDelay(0);
		blinktimer.start();
	}

	protected void fireAction() {
		rounds++;
		if(foreground_mode) {
			this.c.setForeground(rounds % 2 == 0 ? color : this.normalcolor);
		}else{
			this.c.setBackground(rounds % 2 == 0 ? color : this.normalcolor);			
		}
		if (rounds == 9) {
			blinktimer.stop();
			this.blinktimer = null;
			this.c = null;
		}
	}
}
